home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue100 / CPROG17.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  2.9 KB  |  97 lines

  1. /* CPROG17.CPP - Random access to plant pests held in a file */
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <dos.h>
  6. #include <stdlib.h>
  7.  
  8. #define RECLEN 16
  9. #define NUMRECS 10
  10.  
  11. /* Load file 10recs.dat to see the structure - 10 records
  12.    padded to 16 characters each. */
  13.  
  14. void main(void)
  15. {
  16. FILE *fp;    // The file pointer
  17. int c;        // Holds a code that controls the main loop
  18. char kbstring[5], record[RECLEN+1];
  19.  
  20. /* kbstring[] holds the user input: up to 2 digits + '\0' + 1 byte to specify
  21. the maximum length of the input string + 1 byte to report how many were
  22. actually entered=5 bytes total. See Help on cgets().
  23. record[] holds a record read from disk - 16 chars + '\0' */
  24.  
  25.  
  26.     if( ! (fp=fopen("10recs.dat", "r")) ) // Open the data file
  27.         {
  28.         cputs("Can't find data file 10RECS.DAT\n");
  29.         exit(0);
  30.         }
  31.  
  32.     record[RECLEN]='\0'; // Intialise last element in record[] with '\0'
  33.     c=0;             // In case garbage value in c happens to be
  34.                  // ASCII for Q causing premature termination
  35.     while( c != 'Q' )
  36.         {
  37.         clrscr();
  38.         printf("Enter a number from 1 to %d and press [Return]\n", NUMRECS);
  39.         printf("to see an exciting new plant pest!\n");
  40.         printf("Enter [Q][Return] to quit\n");
  41.  
  42.         kbstring[0]=3;    // Allow input of up to 2 chars + '\0'= 3 max
  43.         cgets(kbstring); // Read Help on cgets() for justification of
  44.                 // previous line.
  45.  
  46.         if( kbstring[1] == 0 )    // Filter out invalid input values - like no input. '\0' doesn't count in number of chars read.
  47.             {
  48.             c=0;        // Make sure c is a non-'Q' value
  49.             continue;    // Skip rest of loop and go to while() line
  50.             }
  51.         if( kbstring[1]==1 && (kbstring[2]=='Q' || kbstring[2]=='q') )
  52.             {    // Does the user want to quit?
  53.             c='Q';
  54.             continue;
  55.             }
  56.         c=atoi(&kbstring[2]);    // Attempt to convert input (which starts at element [2] to an integer. See atoi() in Help.
  57.         if( c<1 || c>NUMRECS )
  58.             {
  59.             c=0;
  60.             continue;
  61.             }
  62.  
  63.         // c must now contain a valid value 1-NUMRECS.
  64.  
  65.         fseek(fp, RECLEN*(c-1), SEEK_SET);
  66.             /* Fseek moves the position in the file from which the
  67.             next data will be read. SEEK_SET is a macro which says
  68.             that the distance specified in parameter 2 is from the
  69.             start of the file. Ctrl+Shift+F1 on it to see other
  70.             possibilities. The first byte in the file is at
  71.             offset zero, so record X is at offset RECLEN*X. Since
  72.             c's minimum value is 1, it must be reduced by 1 in the
  73.             calculation */
  74.  
  75.         fread(record, RECLEN, 1, fp);
  76.             /* Another of the many ways to read data from a file.
  77.             fread()'s first parameter is a pointer to a place
  78.             where the read-in data can be placed.
  79.             The second parameter is the size (in bytes) of the
  80.             daat item to be read. Here it is 10 bytes.
  81.             The third parameter is the number of items to read.
  82.             So we're reading 1 object, 10 bytes long, into
  83.             record[]. 10 objects 1 byte long would have the
  84.             same effect. */
  85.  
  86.         printf("\nPest %d is %s\n", c, record);
  87.         delay(2000);
  88.         }
  89.  
  90.     fclose(fp);
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97.